﻿using FANUC.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class TodayAllRobotsOutputsByPartController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(TodayAllRobotsOutputsByPartNumberDistributionDtoOutput))]
        public IHttpActionResult Get()
        {
            #region Test
            if (Helper.IsTest)
            {
                TodayAllRobotsOutputsByPartNumberDistributionDtoOutput testData = new TodayAllRobotsOutputsByPartNumberDistributionDtoOutput();
                testData.Date = System.DateTime.Now.Date;

                testData.SlicesInfo = new List<TodayAllRobotsOutputsByPartNumberDistributionSliceDto>();
                TodayAllRobotsOutputsByPartNumberDistributionSliceDto info1 = new TodayAllRobotsOutputsByPartNumberDistributionSliceDto() { PartNumber = "PN1", Ratio = 10.5M, Quantity = 105 };
                TodayAllRobotsOutputsByPartNumberDistributionSliceDto info2 = new TodayAllRobotsOutputsByPartNumberDistributionSliceDto() { PartNumber = "PN2", Ratio = 20M, Quantity = 200 };
                TodayAllRobotsOutputsByPartNumberDistributionSliceDto info3 = new TodayAllRobotsOutputsByPartNumberDistributionSliceDto() { PartNumber = "PN3", Ratio = 30M, Quantity = 300 };
                TodayAllRobotsOutputsByPartNumberDistributionSliceDto info4 = new TodayAllRobotsOutputsByPartNumberDistributionSliceDto() { PartNumber = "PN4", Ratio = 39.5M, Quantity = 395 };
                testData.SlicesInfo.Add(info1);
                testData.SlicesInfo.Add(info2);
                testData.SlicesInfo.Add(info3);
                testData.SlicesInfo.Add(info4);

                return Ok(testData);
            }
            #endregion

            DateTime today = System.DateTime.Now.Date;
            var allRobotsOutputsByPartSummaryArc = (from w in db.Part_Unit.Where(a => a.Start_Time >= today)
                                                    group w by new { w.Part_Name } into grp
                                                    select new
                                                    {
                                                        grp.Key.Part_Name,
                                                        TotalQuantity = grp.Count()
                                                    }
                                                 );
            var allRobotsOutputsByPartSummarySpot = (from w in db.Spot_Part_Unit.Where(a => a.Start_Time >= today)
                                                     group w by new { w.Part_Name } into grp
                                                     select new
                                                     {
                                                         grp.Key.Part_Name,
                                                         TotalQuantity = grp.Count()
                                                     }
                                                 );
            var allRobotsOutputsByPartSummarySeal = (from w in db.Seal_Part_Unit.Where(a => a.Start_Time >= today)
                                                     group w by new { w.Part_Name } into grp
                                                     select new
                                                     {
                                                         grp.Key.Part_Name,
                                                         TotalQuantity = grp.Count()
                                                     }
                                                );
            var allRobotsOutputsByPartSummaryPolish = (from w in db.Polish_Part_Unit.Where(a => a.Start_Time >= today)
                                                       group w by new { w.Part_Name } into grp
                                                       select new
                                                       {
                                                           grp.Key.Part_Name,
                                                           TotalQuantity = grp.Count()
                                                       }
                                               );
            var allRobotsOutputsByPartSummaryPress = (from w in db.Press_Part_Unit.Where(a => a.Start_Time >= today)
                                                      group w by new { w.Part_Name } into grp
                                                      select new
                                                      {
                                                          grp.Key.Part_Name,
                                                          TotalQuantity = grp.Count()
                                                      }
                                               );
            var allRobotsOutputsByPartSummary = allRobotsOutputsByPartSummaryArc.Concat(allRobotsOutputsByPartSummarySpot).Concat(allRobotsOutputsByPartSummarySeal).Concat(allRobotsOutputsByPartSummaryPolish).Concat(allRobotsOutputsByPartSummaryPress);
            allRobotsOutputsByPartSummary = allRobotsOutputsByPartSummary.OrderByDescending(a => a.TotalQuantity);

            if (allRobotsOutputsByPartSummary.Count() <= 0)
            {
                TodayAllRobotsOutputsByPartNumberDistributionDtoOutput noData = new TodayAllRobotsOutputsByPartNumberDistributionDtoOutput();
                noData.Date = System.DateTime.Now.Date;

                noData.SlicesInfo = new List<TodayAllRobotsOutputsByPartNumberDistributionSliceDto>();
                TodayAllRobotsOutputsByPartNumberDistributionSliceDto info = new TodayAllRobotsOutputsByPartNumberDistributionSliceDto() { PartNumber = "", Ratio = 0M, Quantity = 0 };
                noData.SlicesInfo.Add(info);
                return Ok(noData);
            }

            int todayTotalQuantity = allRobotsOutputsByPartSummary.Sum(a => a.TotalQuantity);

            TodayAllRobotsOutputsByPartNumberDistributionDtoOutput data = new TodayAllRobotsOutputsByPartNumberDistributionDtoOutput();
            data.Date = System.DateTime.Now.Date;
            data.SlicesInfo = new List<TodayAllRobotsOutputsByPartNumberDistributionSliceDto>();
            foreach (var item in allRobotsOutputsByPartSummary)
            {
                data.SlicesInfo.Add(new TodayAllRobotsOutputsByPartNumberDistributionSliceDto() { PartNumber = item.Part_Name, Ratio = Decimal.Round(Convert.ToDecimal(item.TotalQuantity) / todayTotalQuantity, 4), Quantity = item.TotalQuantity });
            }

            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        { return null; }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
